package com.java.mybatis.controller;

import com.java.mybatis.entity.Announcement;
import com.java.mybatis.entity.Employees;
import com.java.mybatis.service.AnnouncementService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (Announcement)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/announcement/")
public class AnnouncementController {
    /**
     * 服务对象
     */
    @Resource
    private AnnouncementService announcementService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public void selectOne(Integer id,HttpServletRequest request) {
        Announcement announcement = announcementService.queryById(id);
        request.getSession().setAttribute("announcement",announcement);
        System.out.println("---------------------------"+announcement);
    }
    /**
     * 查询所有公告信息的方法
     * @return 对象序列
     */
    @RequestMapping("selectAllAnnouncement")
    public String selectAllAnnouncement(Model model){
        //调用查询所有公告信息的方法
        List<Announcement> announcements = announcementService.selectAllAnnouncement();
        //将返回的值存入model中
        model.addAttribute("announcements",announcements);
        //得到总条数的方法
        model.addAttribute("ids",announcements.size());
        //返回对应的页面
        return "System/banner-list";
    }
    /**
     * 新增所有公告信息的方法
     * @return 受影响行数
     */
    @RequestMapping("insertAnnouncement")
    public String insertAnnouncement(HttpServletRequest request, Model model, String announcementTitle, String announcementContent){
        //创建Announcement类的具体实例
        System.out.println(announcementContent+"::-------"+announcementTitle);
        Announcement announcement = new Announcement();
        announcement.setAnnouncementTitle(announcementTitle);//添加标题
        announcement.setAnnouncementContent(announcementContent);//添加公告内容
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//日期格式化
        announcement.setAnnouncementTime(simpleDateFormat.format(new Date()));//默认当前时间
        announcement.setAnnouncementCode(1);//默认发布
        //创建Employees类的具体实例
        Employees employees = (Employees) request.getSession().getAttribute("emp");
        announcement.setEmployees(employees);//添加发布人
        //调用查询所有公告信息的方法
        int i = announcementService.insertAnnouncement(announcement);
        //将返回的数据存入model中
        model.addAttribute("announcements",i);
        //返回对应的页面
        return "redirect:/announcement/selectAllAnnouncement";
    }
    /**
     * 删除的方法
     * @param id
     * @return
     */
    @RequestMapping("upAnnouncementDown")
    public String upAnnouncementDown(Integer id){
        System.out.println("公告编号:"+id);
        try {
            //调用删除的方法
            announcementService.upAnnouncementDown(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/announcement/selectAllAnnouncement";
    }
    /**
     * 批量删除的方法
     * @return
     */
    @RequestMapping("upAnnouncementAll")
    public String upAnnouncementAll(String ids1) throws Exception {
        String[] split = ids1.split(",");
        System.out.println("获取到的所有公告编号:"+ids1);
        for (String s : split) {
            System.out.println("获取到的所有公告编号:"+s);
            announcementService.upAnnouncementDown(Integer.parseInt(s));
        }
        return "redirect:/announcement/selectAllAnnouncement";
    }
    /**
     * 修改公告信息的方法
     * @return 受影响行数
     */
    @RequestMapping("updateAnnouncementById")
    public String updateAnnouncementById(HttpSession session, Model model, Integer announcementId, String announcementTitle, String announcementContent){
        //测试
        System.out.println("获取到前台传过来的公告标题:"+announcementTitle);
        System.out.println("获取到前台传过来的公告内容:"+announcementContent);
        //创建Announcement类的具体实例
        Announcement announcement = new Announcement();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式化时间信息
        announcement.setAnnouncementTime(simpleDateFormat.format(new Date()));//默认当前时间
        announcement.setAnnouncementTitle(announcementTitle);//修改后的标题
        announcement.setAnnouncementContent(announcementContent);//修改后的内容
        announcement.setAnnouncementId(announcementId);
        announcement.setEmployees((Employees) session.getAttribute("emp"));
        //调用修改公告信息的方法
        int i = announcementService.updateAnnouncementById(announcement);
        //将获取到的值存入model中
        model.addAttribute("announcements",i);
        //返回对应的页面
        return "redirect:/announcement/selectAllAnnouncement";
    }

}